<html>
<head >
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script >
<script>
var flag=false;
$(function(){
window.onload=function(){ 
	var cookies = document.cookie.split("; ");//将获得的所有cookie切割成数组 
	for(var i=0;i<cookies.length;i++)
	{
		if(cookies[i].split('=')[0]=="username"){
			document.getElementById("username").innerHTML = cookies[i].split('=')[1]+"<br>";
		}
		if(cookies[i].split('=')[0]=="password"){		
			flag = true;
				var args = "";
				var url = "showposts.php";
				$.post(url,args,function(msg){
		        		//var data = eval("("+msg+")");	
					var list = JSON.stringify(msg).slice(1,-1).split(',');
					for(var i=0;i<list.length;i++)
					$("span#username").append("<a href='http://127.0.0.1:5222/readpost.html?id="+list[i].split(':')[0].slice(1,-1)+"'>"+list[i].split(':')[1].slice(1,-1)+"<br>");				
					//alert(JSON.stringify(msg).slice(1,-1));		
		        	},"json")

		}
	}
}	
})
function checklogin(){
	if(flag==true)
		window.location.href="http://127.0.0.1:5222/post.html";
	else{
		alert("Please sign up in first");
		window.location.href="http://127.0.0.1:5222/login.html";
	}		 
}
function logout(){
	alert("logout");
	 var date = new Date(); 
                date.setTime(date.getTime() - 10000); 
                document.cookie ="username=a; expires=" + date.toGMTString(); 
document.cookie ="userid=a; expires=" + date.toGMTString();
document.cookie ="password=a; expires=" + date.toGMTString();
	window.location.href="http://127.0.0.1:5222/login.html";
}
</script>
<body>	
welcome: <span id="username" ><br><br> </span><br>
<input type="button" style="width:150px" value="发帖" onclick="checklogin()"><br><br>
<input type="button" style="width:150px" value="退出登录" onclick="logout()">
</body>
</html>
